<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>掌上工单</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" href="css/mui.min.css">
		<style type="text/css">
			.mui-bar{
				background-color: rgba(0,153,204,.9);
			}
			.mui-icon{
				color: #FFFFFF;
			}
		</style>	
	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			
			<!--<a id="icon-bars" class="mui-icon mui-icon-bars mui-pull-left"></a>-->
			<a id="left-nav" class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="display: none;"></a>
			
			<a id="search" class="mui-icon mui-icon-search mui-pull-right"></a>
			<h1 class="mui-title" style="color: #FFFFFF;" id='mui-title'>任务大厅</h1>
		</header>
		<script src="js/mui.min.js"></script>
		<script type="text/javascript" src="js/md5.js" ></script>
		<script type="text/javascript" src="js/uuid.js" ></script>
		<script type="text/javascript" src="js/utils.js" ></script>
		<script src="js/update.js"></script>
		<script>
			
			(function($) {
				
			    var main,
			        backButtonPress = 0;
			
			    $.plusReady(function(){
			    	plus.runtime.setBadgeNumber("3");
			    	
			    	console.debug("setBadgeNumber") ;
			        main =  plus.android.runtimeMainActivity();
			    });
			
			    $.back = function() {
			        backButtonPress++;
			        if (backButtonPress > 1) {
			            //退出
			            //plus.runtime.quit();
			            //后台
			            main && main.moveTaskToBack(false);
			        } else {
			            plus.nativeUI.toast('再按一次返回桌面');
			        }
			        setTimeout(function() {
			            backButtonPress = 0;
			        }, 1000);
			        return false;
			    };
			
			}(mui));
			
		</script>
		<script>
			var titles = [];
				titles["home"] = '任务大厅';
				titles["emc"] = '客服工单';
				titles["notice"] = '通知公告';
				titles["setting"] = '系统设置';
				titles['tips'] = '通知设置' ;
				titles['about'] = '关于' ;
			var menu = null,
				main = null,
				waiting = null ;
			var showMenu = false;
			mui.init({
				swipeBack: false,
				statusBarBackground: '#f7f7f7',
				gestureConfig: {
					doubletap: true
				},
				subpages: [{
					id: 'main',
					url: 'main.html',
					styles: {
						top: '45px',
						bottom: 0,
						bounce: 'vertical'
					}
				}]
			});
			mui.plusReady(function() {
				
				
				document.getElementById("search").addEventListener("tap",function(){
					mui.openWindow({
						url:'task-search.html',
						id:'task-search',
						show: {
							aniShow: "pop-in"
						}
					});
				});
				
				
				plus.navigator.closeSplashscreen();
				plus.navigator.setFullscreen(false);
				waiting = plus.nativeUI.showWaiting() ;
				
				//监听网络变化事件
				document.addEventListener("netchange", function () {
				  var networkinfo = plus.networkinfo.getCurrentType() ;
				  if (plus.networkinfo.CONNECTION_NONE == networkinfo || plus.networkinfo.CONNECTION_UNKNOW == networkinfo) {
				    mui.toast("当前暂无网络连接，请打开数据服务!", "提示");
				  }else{
				  	/** 再次联网立即同步client数据*/
				  	xhz.syncClient();
				  }
				}, false);
			    
				//仅支持竖屏显示
				plus.screen.lockOrientation("portrait-primary");
				main = plus.webview.currentWebview();
				main.addEventListener('maskClick', closeMenu);
				//处理侧滑导航，为了避免和子页面初始化等竞争资源，延迟加载侧滑页面；
				setTimeout(function() {
					menu = mui.preload({
						id: 'index-menu',
						url: 'index-menu.html',
						styles: {
							left: 0,
							width: '70%',
							zindex: -1
						},
						show: {
							aniShow: 'none'
						}
					});
				}, 200);
				
				 //点击左上角侧滑图标，打开侧滑菜单；
//				document.querySelector('.mui-icon-bars').addEventListener('tap', function(e) {
//					if (showMenu) {
//						closeMenu();
//					} else {
//						openMenu();
//					}
//				});
				 //敲击顶部导航，内容区回到顶部
				document.querySelector('header').addEventListener('doubletap', function() {
					main.children()[0].evalJS('mui.scrollTo(0, 100)');
				});
				 //主界面向右滑动，若菜单未显示，则显示菜单；否则不做任何操作
				window.addEventListener("swiperight", openMenu);
				 //主界面向左滑动，若菜单已显示，则关闭菜单；否则，不做任何操作；
				window.addEventListener("swipeleft", closeMenu);
				 //侧滑菜单触发关闭菜单命令
				window.addEventListener("menu:close", closeMenu);
				window.addEventListener("menu:open", openMenu);
				
				//关闭加载画面
				window.addEventListener("wait:close", closeWaiting);
				
				window.addEventListener("title:set", setTitle);
				//重写mui.menu方法，Android版本menu按键按下可自动打开、关闭侧滑菜单；
				mui.menu = function() {
					if (showMenu) {
						closeMenu();
					} else {
						openMenu();
					}
				}
				
			    var mainPage = plus.webview.getWebviewById('main') ;
				if(mainPage){
					mui.fire(mainPage,'tips:set',{id:'mui-icon-home',count: 10});
				}
				
				mui.later(function() {
					xhz.syncClient();
				}, 1000);
				
			});
			var isInTransition = false;
			/**
			 * 显示侧滑菜单
			 */
			function openMenu() {
				if (isInTransition) {
					return;
				}
				if (!showMenu) {
					//侧滑菜单处于隐藏状态，则立即显示出来；
					isInTransition = true;
					menu.setStyle({
						mask: 'rgba(0,0,0,0)'
					}); //menu设置透明遮罩防止点击
					menu.show('none', 0, function() {
						//主窗体开始侧滑并显示遮罩
						main.setStyle({
							mask: 'rgba(0,0,0,0.4)',
							left: '70%',
							transition: {
								duration: 150
							}
						});
						mui.later(function() {
							isInTransition = false;
							menu.setStyle({
								mask: "none"
							}); //移除menu的mask
						}, 160);
						showMenu = true;
					});
				}
			};
			/**
			 * 关闭菜单
			 */
			function closeMenu() {
				if (isInTransition) {
					return;
				}
				if (showMenu) {
					//关闭遮罩；
					//主窗体开始侧滑；
					isInTransition = true;
					main.setStyle({
						mask: 'none',
						left: '0',
						transition: {
							duration: 200
						}
					});
					showMenu = false;
					//等动画结束后，隐藏菜单webview，节省资源；
					mui.later(function() {
						isInTransition = false;
						menu.hide();
					}, 300);
				}
			};
			/**
			 * 关闭加载画面
			 */
			function closeWaiting(){
				waiting&&waiting.close();
				waiting=null;
			}
			
			/**
			 * 修改标题
			 */
			function setTitle(e){
				if(e.detail.title){
					if("home" != e.detail.title){
						document.getElementById('search').style.display='none' ;
					}else{
						document.getElementById('search').style.display='block' ;
					}
					document.getElementById('mui-title').innerHTML = titles[e.detail.title] ;
				}
				if(e.detail.back){
					document.getElementById('left-nav').style.display='block' ;
//					document.getElementById('icon-bars').style.display='none' ;
				}else{
//					document.getElementById('icon-bars').style.display='block' ;
					document.getElementById('left-nav').style.display='none' ;
				}
				
			}
			
			var page = mui.preload({
				url: 'login.html',
				id: 'login',
				show:{
					aniShow:'none'
				},
				waiting:{
					autoShow:false
				}
			});
			
			//首页返回键处理
			//处理逻辑：1秒内，连续两次按返回键，则退出应用；
//			var first = null;
//			mui.back = function() {
//				if (showMenu) {
//					closeMenu();
//				} else {
//					//首次按键，提示‘再按一次退出应用’
//					if (!first) {
//						first = new Date().getTime();
//						mui.toast('再按一次退出应用');
//						setTimeout(function() {
//							first = null;
//						}, 1000);
//					} else {
//						if (new Date().getTime() - first < 1000) {
//							plus.runtime.quit();
//						}
//					}
//				}
//			};	
		</script>
	</body>
</html>